#include "Assertx.h"
#include "Type.h"
#include <time.h>
#include <string.h>
#if defined(WIN32) || defined(WIN64)
#include "GameUtil.h"
#elif defined(__linux__)
#include <execinfo.h>
#endif

int g_Command_Assert = 0;
int g_Command_IgnoreMessageBox = FALSE;

VOID __show__(const CHAR* szTemp) {
#if defined(__linux__)
	printf("Assert:%s", szTemp);
#endif

#ifndef GAME_CLIENT
	FILE* f = fopen("./Log/assert.log", "a");
	fwrite(szTemp, 1, strlen(szTemp), f);
	fwrite("\r\n", 1, 2, f);
	fclose(f);
#endif

#if defined(WIN32) || defined(WIN64)
	static MyLock lock;
	if (g_Command_Assert != 1) {
		lock.Lock();
		INT iRet = ::MessageBoxA(NULL, szTemp, "�쳣", MB_OK);
		lock.Unlock();
	}
#elif defined(__linux__)

#endif

#ifdef GAME_CLIENT
	throw (std::string(szTemp));
#else
	throw (1);
#endif
}

VOID __messagebox__(const CHAR * msg) {
	if (g_Command_IgnoreMessageBox)
		return;
#if defined(WIN32) || defined(WIN64)
	::MessageBoxA(NULL, msg, "��Ϣ", MB_OK);
#elif defined(__linux__)
#endif
}

VOID __assert__(const CHAR * file, UINT line, const CHAR * func, const CHAR * expr) {
	CHAR szTemp[1024] = {0};

#if defined(WIN32) || defined(WIN64)
	sprintf(szTemp, "[%s][%d][%s][%s]", file, line, func, expr);
#elif defined(__linux__)
	sprintf(szTemp, "[%s][%d][%s][%s]\n", file, line, func, expr);
#endif
	__show__(szTemp);
}

VOID __assertex__(const CHAR * file, UINT line, const CHAR * func, const CHAR * expr, const CHAR * msg) {
	CHAR szTemp[1024] = {0};

#if defined(WIN32) || defined(WIN64)
	sprintf(szTemp, "[%s][%d][%s][%s]\n[%s]", file, line, func, expr, msg);
#elif defined(__linux__)
	sprintf(szTemp, "[%s][%d][%s][%s]\n[%s]\n", file, line, func, expr, msg);
#endif
	__show__(szTemp);
}

VOID __assertspecial__(const CHAR * file, UINT line, const CHAR * func, const CHAR * expr, const CHAR* msg) {
	CHAR szTemp[1024] = {0};
#if defined(WIN32) || defined(WIN64)
	sprintf(szTemp, "S[%s][%d][%s][%s]\n[%s]", file, line, func, expr, msg);
#elif defined(__linux__)
	sprintf(szTemp, "S[%s][%d][%s][%s]\n[%s]\n", file, line, func, expr, msg);
#endif
	__show__(szTemp);
}

VOID __protocol_assert__(const CHAR * file, UINT line, const CHAR * func, const CHAR * expr) {
	printf("[%s][%d][%s][%s]", file, line, func, expr);
}

